<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/html">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>

    <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
    <!-- 引入样式 -->
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
    <!-- 引入组件库 -->
    <script src="https://unpkg.com/element-ui/lib/index.js"></script>
    <style>

    </style>
</head>
<body>
<div id="appVue">
    <el-form :model="user" label-width="100px" class="demo-ruleForm">
        <el-form-item label="用户名" prop="name">
            <el-input v-model="user.name"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
            <el-input v-model="user.password"></el-input>
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="login">登录</el-button>
            <el-button type="primary" @click="add">注册</el-button>
        </el-form-item>
    </el-form>

   <!-- <el-dialog title="收货地址" :visible.sync="formLabelWidthss">
        <el-form :model="user">
            <el-form-item label="账号" :label-width="formLabelWidth">
                <el-input v-model="user.name" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="密码" :label-width="formLabelWidth">
                <el-input v-model="user.password" autocomplete="off"></el-input>
            </el-form-item>

        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button @click="formLabelWidthss = false">取 消</el-button>
            <el-button type="primary" @click="findUsers">确 定</el-button>
        </div>
    </el-dialog>-->
    <template>
        <el-dialog title="收货地址" :visible.sync="formLabelWidthss">
            <el-form :model="user" :rules="rules" ref="userForm">
                <el-form-item label="账号" :label-width="formLabelWidth" prop="name">
                    <el-input v-model="user.name" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="密码" :label-width="formLabelWidth" prop="password">
                    <el-input v-model="user.password" autocomplete="off" type="password"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="formLabelWidthss = false">取 消</el-button>
                <el-button type="primary" @click="submitForm('userForm')">确 定</el-button>
            </div>
        </el-dialog>
    </template>
</div>
</body>
<script>
    let appVue =new Vue({
        el:'#appVue',
        data:{
            user:{
                name:'',
                password:'',
            },
            formLabelWidthss:false,
            rules: {
                name: [
                    { required: true, message: '请输入账号', trigger: 'blur' },
                    { min: 5, max: 10, message: '账号长度在 5 到 10 个字符', trigger: 'blur' }
                ],
                password: [
                    { required: true, message: '请输入密码', trigger: 'blur' },
                    { min: 5, max: 10, message: '密码长度在 5 到 10 个字符', trigger: 'blur' }
                ]
            }
        },
        methods:{
            login() {
                $.ajax({
                    // 用post方式进行提交
                    type: 'post',
                    url: '/user/login',
                    dataType: 'json',
                    data: JSON.stringify(this.user),
                    contentType: 'application/json',
                    success: function (response) {
                        if (response.status==="success"){
                            location.href="scuess.html"
                        }

                    }
                })
            },
            add(){
               appVue.formLabelWidthss = true;
            },
            findUsers(){
                $.ajax({
                    // 用post方式进行提交
                    type: 'post',
                    url: '/user/add',
                    dataType: 'json',
                    data: JSON.stringify(this.user),
                    contentType: 'application/json',
                    success: function (data) {
                        if(data == 1){
                            appVue.formLabelWidthss = false;
                            appVue.user={};
                            alert("注册成功")
                        }else {
                            alert("注册失败")
                        }
                    }
                })
            },
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                       this.findUsers()
                    } else {
                        alert("请正确输入值")
                        return false;
                    }
                });
            },
        }
    })
</script>
</html>